#ifndef _FLV_H
#define _FLV_H 1

/*
FIXME currently little endian platform only

*/

typedef unsigned char u8;
typedef unsigned int u32;
typedef unsigned short u16;
typedef unsigned int u24;
typedef signed int s32;


/* FLV Format Spec:
http://www.adobe.com/devnet/flv/pdf/video_file_format_spec_v10.pdf
*/
typedef struct {
  u8 sign[3];
  u8 ver;

  u32 has_video:1;
  u32 res2:1;
  u32 has_audio:1;
  u32 res1:5;

  u32 doff;
} __attribute__((packed)) flvhdr;

typedef struct {
  u8 type;     /* 8:audio; 9:video; 18:script data */
#define FLVTAG_AUDIO 8
#define FLVTAG_VIDEO 9
#define FLVTAG_SCRIPT 18
  u32 ds:24;   /* data size */
  u32 ts:24;   /* timestamp (msec) */
  u8 tse;      /* timestamp extended */
  u32 id:24;   /* stream id === 0 */
} __attribute__((packed)) flvtaghdr;

typedef struct {
  u8 type:1;
#define FLVAUDIO_MONO    0
#define FLVAUDIO_STEREO  1
  u8 depth:1;
#define FLVAUDIO_8BIT    0
#define FLVAUDIO_16BIT   1
  u8 rate:2;
#define FLVAUDIO_5512HZ   0
#define FLVAUDIO_11025HZ  1
#define FLVAUDIO_22050HZ  2
#define FLVAUDIO_44100HZ  3
  u8 format:4;
#define FLVAUDIO_PCM                    0
#define FLVAUDIO_ADPCM                  1
#define FLVAUDIO_MP3                    2
#define FLVAUDIO_PCM_LE                 3
#define FLVAUDIO_NELLYMOSER_16KHZ_MONO  4
#define FLVAUDIO_NELLYMOSER_8KHZ_MONO   5
#define FLVAUDIO_NELLYMOSER             6
#define FLVAUDIO_AAC                    10
#define FLVAUDIO_SPEEX                  11
} __attribute__((packed)) flvaudiohdr;

typedef struct {
  u8 codec:4;
#define FLVVIDEO_JPEG  1
#define FLVVIDEO_H263  2
#define FLVVIDEO_FSV1  3
#define FLVVIDEO_VP6   4
#define FLVVIDEO_VP6A  5
#define FLVVIDEO_FSV2  6
#define FLVVIDEO_AVC   7
  u8 type:4;
#define FLVVIDEO_KEY        1
#define FLVVIDEO_INTER      2
#define FLVVIDEO_DISPINTER  3
#define FLVVIDEO_GENKEY     4
#define FLVVIDEO_COMMAND    5
} __attribute__((packed)) flvvideohdr;


/**  endian conv utils  */

#define bswap32(i)\
  (((i) << 24) | (((i) << 8) & 0xff0000) | (((i) >> 8) & 0xff00) | ((i) >> 24))

#define bswap24(i)\
  ((((i) & 0xff) << 16) | ((i) & 0xff00) | (((i) >> 16) & 0xff))

#endif /* _FLV_H */
